package
{
	import code.main.App;
	import code.main.AppData;
	import code.structure.manager.loader.DllLoaderManager;
	import code.tools.gameIni.GameIniData;
	
	import com.loadPackage.BulkLoader;
	import com.loadPackage.BulkProgressEvent;
	import com.patience.net.Net;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.system.Security;
	import flash.text.TextField;

	[SWF(width = 1000, height = 650, backgroundColor = 0xcccccc)]
	/**
	 * 入口文件 
	 * @author Yuri
	 * 
	 */
	public class entry extends Sprite
	{
		public function entry()
		{
			init();
		}
		private function init():void{
			App.appstage = this.stage;
			systemSetting();
			start();
		}
		
		private function start():void
		{
			// TODO Auto Generated method stub
			//flashvas
			AppData.analysisSwfParameters(this.loaderInfo.parameters);
			startIni();
		}
		private var _bLoader:BulkLoader;
		/**
		 *加载配置文件 
		 * 
		 */
		private function startIni():void
		{
			// TODO Auto Generated method stub
			_bLoader = new BulkLoader("gameIni");	
			_bLoader.add(GameIniData.gameIniPath);
			_bLoader.start();
			_bLoader.addEventListener(BulkProgressEvent.COMPLETE,loadGameIniComplete);
		}
		
		protected function loadGameIniComplete(event:BulkProgressEvent):void
		{
			GameIniData.analysisGameXml(_bLoader.getXML(GameIniData.gameIniPath));
			BulkLoader.removeAllLoaders(); //清空
			DllLoaderManager.getInstance().loadLoading(loadingComplete);
		}
		
		private var _loading:MovieClip;
		/**
		 *loading进度条 
		 */
		private var _bar:MovieClip ;
		/**
		 * loading加载进度文本 
		 */
		private var _txt:TextField;
		private function loadingComplete():void
		{
			// TODO Auto Generated method stub
			_loading = new (DllLoaderManager.getInstance().getMaterial("loading","LOADING") as Class) as MovieClip;
			if(!_loading) return;
			//loading加载完毕
			
			_bar = _loading.getChildByName("progressBar_mc") as MovieClip;
			_txt = _loading.getChildByName("progressBar_txt") as TextField;
			
			_bar.gotoAndStop(1);
			App.appstage.addChild(_loading);
			
			App.appstage.addEventListener(Event.RESIZE,onResizeHandler);
			onResizeHandler();
			
			DllLoaderManager.getInstance().loadFirstFiles(onFirstLoadCompleteHandler,onFirstLoadProgressHandler);
		}
		/**
		 *首次全部加载完毕 
		 * @param event
		 * 
		 */
		protected function onFirstLoadCompleteHandler(mainS:MovieClip):void
		{
			if(mainS){
				Net.getIntance().addEventListener("enter_game",initOverHandler);
				Net.getIntance().addEventListener("config_over",configoverHandler);
				App.appstage.addChildAt(mainS,0);
			}
		}
		
		private function configoverHandler(e:Event):void{
			if(_txt) _txt.text = "100%";
			Net.getIntance().removeEventListener("config_over",configoverHandler);
		}
		
		private function initOverHandler(event:Event):void
		{
			// TODO Auto-generated method stub
			Net.getIntance().removeEventListener("initover",initOverHandler);
			if(_loading && _loading.parent){
				App.appstage.removeChild(_loading);
				_loading = null;
				_bar = null;
				_txt = null;
				App.appstage.removeEventListener(Event.RESIZE,onResizeHandler);
			}
			this.graphics.clear();
		}
		
		/**
		 *加载进度 
		 * @param pro
		 */
		protected function onFirstLoadProgressHandler(pro:Number):void
		{
			//to do 改变loading的进度
			if(!_bar || !_txt){
				return;
			}
			pro = pro>1?1:pro;
			var frame:int = pro*_bar.totalFrames;
			_bar.gotoAndStop(frame);
			var temp:int = pro*100;
			if(temp >=100) temp = 99;
			_txt.text = temp+ "%";
		}
		/**
		 *自适应 
		 * @param e
		 * 
		 */
		private function onResizeHandler(e:Event = null):void{
			if(!_loading) return;
			_loading.x = (App.appstage.stageWidth - LOADING_WEIGHT)/2;
			_loading.y = (App.appstage.stageHeight - LOADING_HEIGHT)/2;
			this.graphics.clear();
			this.graphics.beginFill(0xfffbf2);
			this.graphics.drawRect(0,0,App.appstage.stageWidth,App.appstage.stageHeight);
			this.graphics.endFill();
		}
		/**
		 *Loading原始大小 宽度 
		 */
		public static const LOADING_WEIGHT:Number = 1000;
		
		/**
		 *Loading原始大小 高度 
		 */
		public static const LOADING_HEIGHT:Number = 712;
		
		private function systemSetting():void
		{
			// TODO Auto Generated method stub
			stage.scaleMode = 'noScale';
			stage.align = 'TL';
			stage.tabChildren = false;
			stage.frameRate = 30;
			Security.allowDomain("*");
			Security.allowInsecureDomain("*");
			App.appstage.showDefaultContextMenu = false;
			Security.loadPolicyFile("https://facebook.com/crossdomain.xml");
		}
	}
}